package com.cheetah.dao.impl;

import com.cheetah.constants.KeyConstants;
import com.cheetah.dao.MessageDAO;
import com.cheetah.entity.GroupMessage;
import com.cheetah.entity.Message;
import com.cheetah.support.redis.RedisClient;
import com.cheetah.utils.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

/**
 * @Author Administrator
 * @Date 2020/9/2
 * @Version V1.0
 * @Description:
 **/
@Repository
public class MessageDAOImpl implements MessageDAO {

    @Autowired
    @Qualifier("REDIS_IM")
    private RedisClient redisClient;

    @Autowired
    @Qualifier("MONGO_IM")
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Message message){
        redisClient.hSet(KeyConstants.KEY_MESSAGE_H,String.valueOf(message.getId()),message);

        mongoTemplate.save(message);
    }

    @Override
    public void save(GroupMessage groupMessage){
        redisClient.zSet(KeyUtil.generate(KeyConstants.KEY_GROUP_MESSAGE_Z,groupMessage.getGroupId()),
                String.valueOf(groupMessage.getMsgId())
                ,groupMessage.getScore());
        mongoTemplate.save(groupMessage);
    }
}
